package com.dst.smart.handler.disposer;

import android.content.Context;
import android.util.Log;
import com.dst.smart.app.MainActivity;
import com.dst.smart.handler.entity.SemanticResult;
import com.dst.smart.handler.entity.ServiceType;
import com.dst.smart.player.entity.SongPlayInfo;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 *	星座
 *	白羊座今天的运势
 	摩羯座这周运势
 	射手座的爱情运势
 * author <a href="http://www.xfyun.cn">讯飞开放平台</a>
 * date 2016年9月5日 下午4:58:10
 *
 */
public class ConstellationDisposer extends Disposer {
	//private static final String KEY_URL = "url";

	public ConstellationDisposer(Context context) {
		super(context);
	}
	
	@Override
	public void disposeResult(final SemanticResult semanticResult) {
		try {
			if(semanticResult!=null) {
				JSONArray result = semanticResult.getResult();
				if (result != null && result.length() > 0) {
					//MainActivity.playSound(5, semanticResult.getAnswerText());
					List<SongPlayInfo> playList = new ArrayList<SongPlayInfo>();
					//String resultTxt=semanticResult.getAnswerText();
					SongPlayInfo songPlayInfo;
					if (semanticResult.getAnswerText().length() > 0) {
						songPlayInfo = new SongPlayInfo();
						songPlayInfo.setPlayUrl(semanticResult.getAnswerText());
						playList.add(songPlayInfo);
					}
					for (int i = 0; i < result.length(); i++) {
						JSONObject newsJson = result.optJSONObject(i);
						JSONArray newsArray = newsJson.getJSONArray("fortune");
						for (int j = 0; j < newsArray.length(); j++) {
							JSONObject jsonObject = newsArray.getJSONObject(j);
							String star = "";
							String fortune = "";
							String description = "";
							if (jsonObject.has("name")) {
								fortune = jsonObject.getString("name");
							}
							if (jsonObject.has("star")) {
								star = jsonObject.getString("star") + "星指数";
							}
							if (jsonObject.has("description")) {
								description = jsonObject.getString("description");
							}
							//resultTxt+=fortune+star+description;
							songPlayInfo = new SongPlayInfo();
							songPlayInfo.setPlayUrl(fortune + star + description);
							playList.add(songPlayInfo);
						}
					}
					getPlayController().playURLList(semanticResult.getUUID(), playList);
					getPlayController().playText(semanticResult.getUUID(), playList.get(0).getPlayUrl(), true, "", null);

				} else {
					getPlayController().playURLList(semanticResult.getUUID(), null);
					getPlayController().playText(semanticResult.getUUID(), semanticResult.getAnswerText(), true, "", null);
				}
				sendAiuiData("星座运势", "constellation", result.toString());
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}
	
	public boolean canHandle(ServiceType type){
		return (type == ServiceType.CONSTELLATION);
	}
}
